package service

import (
	"context"
	"fx_swift_boss/api"
	"fx_swift_boss/pkg/code"
	"fx_swift_boss/pkg/global"
	"fx_swift_boss/pkg/internalerrors"
	"fx_swift_boss/pkg/page"
	"github.com/spf13/cast"
)

type IPayeeSrv interface {
	QueryDomesticPayee(ctx context.Context, req *api.PayeeReq) (*page.Collection[*api.OperatorPayeeRes], error)
	QueryInternationPayee(ctx context.Context, req *api.PayeeReq) (*page.Collection[*api.PayeeInternationRes], error)
}

type payeeSrv struct {
}

func NewPayeeSrv() IPayeeSrv {
	return &payeeSrv{}
}

// 国际收款人列表
func (p payeeSrv) QueryInternationPayee(ctx context.Context, req *api.PayeeReq) (*page.Collection[*api.PayeeInternationRes], error) {
	var (
		err        error
		resultList []*api.PayeeInternationRes
		total      int
	)

	collectionDto := &page.Collection[*api.PayeeInternationRes]{}
	collectionDto.Page = req.PageNo
	collectionDto.PageSize = req.PageSize

	mod := global.GFDB.Ctx(ctx).Schema("fx").Model("ifcgate_internation_payee")
	if len(req.BankAcctNo) > 0 {
		mod.Where("payee_bank_account_no like ?", "%"+req.BankAcctNo+"%")
	}
	if len(req.Currency) > 0 {
		mod.Where("arrive_currency=?", req.Currency)
	}
	if len(req.StartTime) > 0 {
		mod.Where("created_at>=?", req.StartTime+" 00:00:00")
	}
	if len(req.EndTime) > 0 {
		mod.Where("created_at<=?", req.EndTime+" 23:59:59")
	}

	if len(req.SortFields) > 0 && len(req.SortMethod) > 0 {
		mod.Order(req.SortFields + " " + req.SortMethod)
	}
	mod.Order("created_at desc")
	mod.Limit(int(page.PageToOffset(req.PageNo, req.PageSize)), cast.ToInt(req.PageSize))
	err = mod.ScanAndCount(&resultList, &total, false)
	collectionDto.TotalPage = page.GetTotalPage(req.PageNo, req.PageSize, cast.ToInt64(total))

	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, "数据查询异常")
	}
	collectionDto.Total = cast.ToInt64(total)
	collectionDto.List = resultList

	return collectionDto, err
}

// 国内收款人列表
func (p payeeSrv) QueryDomesticPayee(ctx context.Context, req *api.PayeeReq) (*page.Collection[*api.OperatorPayeeRes], error) {
	var (
		err        error
		resultList []*api.OperatorPayeeRes
		total      int
	)

	collectionDto := &page.Collection[*api.OperatorPayeeRes]{}
	collectionDto.Page = req.PageNo
	collectionDto.PageSize = req.PageSize

	mod := global.GFDB.Ctx(ctx).Schema("fx").Model("ifcgate_domestic_payee")
	if len(req.BizFlow) > 0 {
		mod.Where("biz_flow like ?", "%"+req.BizFlow+"%")
	}
	if len(req.BankAcctNo) > 0 {
		mod.Where("bank_acct_no like ?", "%"+req.BankAcctNo+"%")
	}
	if len(req.Currency) > 0 {
		mod.Where("currency=?", req.Currency)
	}
	if len(req.StartTime) > 0 {
		mod.Where("created_at>=?", req.StartTime+" 00:00:00")
	}
	if len(req.EndTime) > 0 {
		mod.Where("created_at<=?", req.EndTime+" 23:59:59")
	}

	if len(req.SortFields) > 0 && len(req.SortMethod) > 0 {
		mod.Order(req.SortFields + " " + req.SortMethod)
	}
	mod.Order("created_at desc")
	mod.Limit(int(page.PageToOffset(req.PageNo, req.PageSize)), cast.ToInt(req.PageSize))
	err = mod.ScanAndCount(&resultList, &total, false)
	collectionDto.TotalPage = page.GetTotalPage(req.PageNo, req.PageSize, cast.ToInt64(total))

	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, "数据查询异常")
	}

	collectionDto.Total = cast.ToInt64(total)
	collectionDto.List = resultList

	return collectionDto, err
}
